﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tilver.Coder.Core;

namespace Tilver.Coder.Win.Views
{
    public partial class TableView : UserControl
    {
        private CoderTabular _tabular;
        public CoderTabular Tabular
        {
            get { return _tabular; }
            set
            {
                _tabular = value;
                LoadTableInfo();
            }
        }
        public TableView()
        {
            InitializeComponent();
            gvIndex.AutoGenerateColumns = false;
            gvForeignKeys.AutoGenerateColumns = false;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            _tabular.Name = textBoxName.Text;
            _tabular.Description = textBoxDescription.Text;
            _tabular.Owner = textBoxOwner.Text;
            buttonSave.Enabled = false;
        }
        /// <summary>
        /// Tab标签变换时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabControlTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (tabControlTable.SelectedIndex)
            {
                case 0:
                    LoadColumnInfo();
                    break;
                case 1://加载列信息
                    LoadColumnInfo();
                    break;
                case 2://加载外键信息
                    LoadForeignKey();
                    break;
                case 3://加载索引信息
                    LoadIndex();
                    break;
                case 4://加载代码
                    LoadCode();
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 加载基本信息
        /// </summary>
        private void LoadTableInfo()
        {
            textBoxName.Text = _tabular.Name;
            //textBoxNameSpace.Text = _tabular.DataBase.NameSpace;
            //textBoxNameSpace.Text = _tabular.NameSpace;
            textBoxDescription.Text = _tabular.Description;
            textBoxOwner.Text = _tabular.Owner;
        }
        /// <summary>
        /// 加载
        /// </summary>
        private void LoadCode()
        {
            var engineFactory = new Engine.EngineFactory();
            var engine = engineFactory.GetEngine(_tabular);
            cteCode.Text = engine.RenderToString();
        }

        /// <summary>
        /// 加载索引信息
        /// </summary>
        private void LoadIndex()
        {
            if (_tabular == null || _tabular.Columns == null || _tabular.TabularType != TabularTypeEnum.Table) return;
            var table = _tabular as CoderTable;
            gvIndex.DataSource = table.Indexes;
        }
        /// <summary>
        /// 加载外键信息
        /// </summary>
        private void LoadForeignKey()
        {
            if (_tabular == null || _tabular.Columns == null || _tabular.TabularType != TabularTypeEnum.Table) return;
            var table = _tabular as CoderTable;
            gvForeignKeys.DataSource = table.ForeignKeys;
        }
        /// <summary>
        /// 加载列信息
        /// </summary>
        private void LoadColumnInfo()
        {
            if (_tabular == null || _tabular.Columns == null) return;
              columnViewTableColumns.DataObjects = _tabular.Columns;
        }

        private void gvForeignKeys_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e == null || e.Value == null || !(sender is DataGridView)) return;
            var cell = gvForeignKeys[e.ColumnIndex, e.RowIndex];
            var tableKey = cell.OwningRow.DataBoundItem as CoderTableKey;
            switch (gvForeignKeys.Columns[e.ColumnIndex].DataPropertyName)
            {
                case "PrimaryColumnNames":
                    e.Value = string.Join(",", tableKey.PrimaryColumnNames);
                    break;
                case "ForeignColumnNames":
                    e.Value = string.Join(",", tableKey.ForeignColumnNames);
                    break;
            }
        }

        private void gvIndex_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e == null || e.Value == null || !(sender is DataGridView)) return;
            if (!gvIndex.Columns[e.ColumnIndex].Name.Equals("Columns")) return;
            var cell = gvIndex[e.ColumnIndex, e.RowIndex];
            var index = cell.OwningRow.DataBoundItem as CoderIndex;
            cell.Value = string.Join(",", index.MemberColumnNames);
        }

    }
}
